package com.dhl.deliver.service;

import com.dhl.deliver.entity.Image;
import com.dhl.deliver.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class FilePathService {

    public String Upload(@RequestParam("file")MultipartFile file){
        if(!file.isEmpty()){
            String fileName=file.getOriginalFilename();
            String dir= ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static";
            try {
                FileUtil.fileupload(file.getBytes(),dir,fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return fileName;
        }
        else return "fail";
    }

    public String deleteFile(String filepath){
        String dir= ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static";
        if(FileUtil.isExist(filepath,dir)){
            FileUtil.deleteFile(filepath,dir);
            return "删除成功";
        }
        else return "删除失败";

    }
}
